package com.zensun.web.controller.system;

import com.zensun.common.annotation.Log;
import com.zensun.common.core.controller.BaseController;
import com.zensun.common.core.domain.BaseEntity;
import com.zensun.common.core.domain.R;
import com.zensun.common.core.page.PageDataInfo;
import com.zensun.common.enums.BusinessType;
import com.zensun.system.domain.SysDictType;
import com.zensun.system.domain.dto.SysDictTypeDto;
import com.zensun.system.domain.vo.SysDictTypeSelectVo;
import com.zensun.system.domain.vo.SysDictTypeVo;
import com.zensun.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author gmk
 */
@Api(value = "数据字典类型管理", tags = "数据字典类型模块")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("查询数据字典类型列表")
    @PreAuthorize("@ss.hasPermi('system:dicttype:list')")
    @GetMapping("/list")
    public R<PageDataInfo<SysDictTypeVo>> list(SysDictTypeDto dictTypeDto) {
        startPage();
        return R.success(getPageDataTable(dictTypeService.selectDictTypeList(dictTypeDto)));
    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation("查询字典类型详细")
    @PreAuthorize("@ss.hasPermi('system:dicttype:query')")
    @GetMapping(value = "/{dictId}")
    public R<SysDictTypeVo> getInfo(@PathVariable Long dictId) {
        return R.success(dictTypeService.selectDictTypeId(dictId));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PreAuthorize("@ss.hasPermi('system:dicttype:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated(BaseEntity.AddGrop.class) @RequestBody SysDictType dict) {
        return R.toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PreAuthorize("@ss.hasPermi('system:dicttype:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated(BaseEntity.UpdateGrop.class) @RequestBody SysDictType dict) {
        return R.toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PreAuthorize("@ss.hasPermi('system:dicttype:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@ApiParam("ID集合（1,2）") @PathVariable Long[] dictIds) {
        return R.toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 清空缓存
     */
    @ApiOperation("清空缓存")
    @PreAuthorize("@ss.hasPermi('system:dicttype:clear')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public R clearCache() {
        dictTypeService.clearCache();
        return R.success();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionselect")
    public R<List<SysDictTypeSelectVo>> optionselect() {
        List<SysDictTypeSelectVo> dictTypes = dictTypeService.optionselect();
        return R.success(dictTypes);
    }
}
